
//drawEle[
  //{type:"",x:100,y:100,width:100,height:100,url:"",fontSize:10,color:"#333333",text:""}
  //]
  //type 需要画的元素 支持img （图片）、 txt（文字）、dashLine（虚线）
  //x 横坐标位置 y纵坐标位置  width 元素的宽 元素的高height   fontSize 字体大小 color 字体颜色/线条颜色 text 文字内容
function drawPostDetailCommon(ctx, drawEle, idx, drawWidth, drawHeight, callback) {
  let _this = this;
  console.log("--------------curIndex" + idx)
  if (idx == drawEle.length) {
    ctx.draw();
    setTimeout(function () {
      callback();
    }, 400)
    return;
  }
  let ele = drawEle[idx++]
  switch (ele.type) {
    case "img":
      wx.downloadFile({
        url: ele.url,
        success: function (res) {
          ctx.drawImage(res.tempFilePath, ele.x, ele.y, ele.width, ele.height);
          console.log("suc " + (idx - 1))
        },
        fail: function (e) {
          console.log(e)
        },
        complete: function () {
          console.log("com " + (idx - 1));
          _this.drawPostDetailCommon(ctx, drawEle, idx, drawWidth, drawHeight, callback);
        }
      });
      break;
    case "txt":
      ctx.setFillStyle(ele.color);
      ctx.setFontSize(ele.fontSize);
      ctx.fillText(ele.text||'', ele.x, ele.y);
      _this.drawPostDetailCommon(ctx, drawEle, idx, drawWidth, drawHeight, callback);
      break;
    case "txt2":
      ctx.setFillStyle(ele.color);
      ctx.setFontSize(ele.fontSize);
      let chr = ele.text.split("");
      let row = [];
      let temp = "";
      for (let i = 0; i < chr.length; i++) {
        if (ctx.measureText(temp).width < ele.width) {
          temp += chr[i];
        } else {
          i--;
          row.push(temp);
          temp = "";
        }
      }
      row.push(temp);
      if (row.length > 2) {//最多换行2行
        let rowCut = row.slice(0, 2);
        let rowPart = rowCut[1];
        let test = "";
        let empty = [];
        for (let i = 0; i < rowPart.length; i++) {
          if (ctx.measureText(test).width < ele.width - 20) {
            test += rowPart[i];
          } else {
            break;
          }
        }
        empty.push(test);
        let group = empty[0] + "..."//这里只显示两行，超出的用...表示
        rowCut.splice(1, 1, group);
        row = rowCut;
      }
      for (let i = 0; i < row.length; i++) {
        ctx.fillText(row[i], ele.x, ele.y + i * ele.lineH);
      }
      _this.drawPostDetailCommon(ctx, drawEle, idx, drawWidth, drawHeight, callback);
      break;
    case "dashLine":
      ctx.setLineWidth(1)
      ctx.setStrokeStyle(ele.color);
      ctx.beginPath();
      ctx.setLineDash([4, 2]);
      ctx.moveTo(ele.x, ele.y);
      ctx.lineTo(ele.toX, ele.toY);
      ctx.stroke();
      _this.drawPostDetailCommon(ctx, drawEle, idx, drawWidth, drawHeight, callback);
      break;
    case "solid":
      ctx.setLineWidth(1)
      ctx.setStrokeStyle(ele.color);
      ctx.beginPath();
      ctx.moveTo(ele.x, ele.y);
      ctx.lineTo(ele.toX, ele.toY);
      ctx.stroke();
      _this.drawPostDetailCommon(ctx, drawEle, idx, drawWidth, drawHeight, callback);
      break;
    case "base64Img":
      wx.downloadFile({
        url: ele.url,
        success: function (res) {
          ctx.drawImage(res.tempFilePath, ele.x, ele.y, ele.width, ele.height);
          console.log("suc " + (idx - 1));
        },
        fail: function (e) {
          console.log(e)
        },
        complete: function () {
          console.log("com " + (idx - 1));
          _this.drawPostDetailCommon(ctx, drawEle, idx, drawWidth, drawHeight, callback);
        }
      });
      break;
    case "rect":
      ctx.setFillStyle(ele.color);
      ctx.fillRect(ele.x, ele.y, ele.width, ele.height);
      _this.drawPostDetailCommon(ctx, drawEle, idx, drawWidth, drawHeight, callback);
      break;
  }
}


module.exports = {
  drawPostDetailCommon
}